/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;

/**
 * File editor
 * 
 * @author Bernard Bou
 */
class LongTextEditor extends AbstractCellEditor implements TableCellEditor
{
	private static final long serialVersionUID = 1L;

	/**
	 * Text
	 */
	private String theText;

	/**
	 * Dialog
	 */
	private TextDialog theTextDialog;

	/**
	 * Button component
	 */
	private JButton theButton;

	/**
	 * Constructor
	 */
	public LongTextEditor()
	{
		theButton = new JButton();
		theButton.setBorderPainted(false);
		theButton.setHorizontalAlignment(SwingConstants.LEFT);
		theButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theTextDialog.setText(theText);
				theTextDialog.setModal(true);
				theTextDialog.setVisible(true);

				// dialog returns control
				fireEditingStopped();
			}
		});

		// text
		theTextDialog = new TextDialog( //

				// ok listener
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						fireEditingStopped();
					}
				},

				// cancel
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						fireEditingCanceled();
					}
				});
	}

	// I N T E R F A C E
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
	 */
	public Component getTableCellEditorComponent(JTable thisTable, Object thisValue, boolean thisIsSelected, int thisRow, int thisColumn)
	{
		theText = (String) thisValue;
		theButton.setText(theText);
		return theButton;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.CellEditor#getCellEditorValue()
	 */
	public Object getCellEditorValue()
	{
		String thisText = theTextDialog.getText();
		return thisText == null || thisText.isEmpty() ? null : thisText;
	}
}
